from lxml import etree
from framework.common import util
from framework.core.crawler_result import Result


class DpParser(object):
    @staticmethod
    def parse(response, current_task):
        parent_result = current_task.parent_result
        content = response.content
        tree = etree.HTML(content)
        detail = dict()

        node_path_url_list = tree.xpath("//div[@id='wayfinding-breadcrumbs_feature_div']/ul/li/descendant::a/@href")
        node_id_path_list = [util.fregex("node=(.*?)$", url, 1) for url in node_path_url_list]
        detail["nodeIdPath"] = ">".join(node_id_path_list)
        detail["nodeId"] = node_id_path_list[-1] if len(node_id_path_list) > 0 else ""

        node_name_path_list = tree.xpath("//div[@id='wayfinding-breadcrumbs_feature_div']/ul/li/descendant::text()")
        node_name_path_list = util.fstring(node_name_path_list).split("›")
        leaf_node_name = node_name_path_list[-1]
        detail["nodeNamePath"] = ">".join(node_name_path_list)
        detail["nodeName"] = leaf_node_name

        div_centerCol = tree.xpath("//div[@id='ppd']/div[@id='centerCol']")[0]
        title = div_centerCol.xpath("//h1[@id='title']/span[@id='productTitle']/text()")
        title = util.fstring(title)
        detail["title"] = title

        feature_bullet_list = div_centerCol.xpath("//div[@id='feature-bullets']//li/span[@class='a-list-item']/text()")
        detail["feature_bullet_list"] = feature_bullet_list

        seller = div_centerCol.xpath("//a[@id='bylineInfo']/text()")
        seller_url = div_centerCol.xpath("//a[@id='bylineInfo']/@href")
        detail["seller_url"] = seller_url

        product_description = div_centerCol.xpath("//div[@id='aplus3p_feature_div']//*[not(self::style) and not(self::script)]/text()")
        product_description = util.fnewlines(product_description)
        detail["product_description"] = product_description

        image_link_list = div_centerCol.xpath("//div[@id='altImages']/ul/li//img/@src")
        detail["image_link_list"] = image_link_list

        rating = div_centerCol.xpath("//div[@id='averageCustomerReviews']//span[@id='acrPopover']/@title")
        rating_count = div_centerCol.xpath("//div[@id='averageCustomerReviews']//span[@id='acrCustomerReviewText']/text()")
        detail["rating_count"] = rating_count

        questions_answered_count = div_centerCol.xpath("//div[@id='ask_feature_div']//text()")
        questions_answered_count = util.fnewlines(questions_answered_count)
        detail["questions_answered_count"] = questions_answered_count

        star_pct = div_centerCol.xpath("//table[@id='histogramTable']//tr[@class='a-histogram-row a-align-center']//text()")
        star_pct = util.fnewlines(star_pct)
        detail["star_pct"] = star_pct

        price = div_centerCol.xpath("//tr[@id='priceblock_ourprice_row']//span[@id='priceblock_ourprice']/text()")
        price = util.fstring(price)
        detail["price"] = price

        shipping_message1 = div_centerCol.xpath("//tr[@id='priceblock_ourprice_row']//span[@id='ourprice_shippingmessage']//*[not(self::script)]/text()")
        shipping_message1 = util.fnewlines(shipping_message1)

        shipping_message2 = div_centerCol.xpath("//tr[@id='priceblock_saleprice_row']//span[@id='saleprice_shippingmessage']//*[not(self::script)]/text()")
        shipping_message2 = util.fnewlines(shipping_message2)

        is_free_shipping = "FREE Shipping" in shipping_message1 or "FREE Shipping" in shipping_message2
        detail["is_free_shipping"] = is_free_shipping
        is_free_returns = "FREE Returns" in shipping_message1 or "FREE Returns" in shipping_message2
        detail["is_free_returns"] = is_free_returns

        amazon_choice = div_centerCol.xpath("//div[@id='acBadge_feature_div']")
        amazon_choice = (len(amazon_choice) > 0)
        detail["amazon_choice"] = amazon_choice

        frequently_bought_together_asin = div_centerCol.xpath("//form[@id='sims-fbt-form']//ul/li[boolean(@data-p13n-asin-metadata)]/@data-p13n-asin-metadata")
        frequently_bought_together_asin = util.fjsonarray(frequently_bought_together_asin, "asin")
        detail["frequently_bought_together_asin"] = frequently_bought_together_asin

        also_bought = div_centerCol.xpath("//div[@id='desktop-dp-sims_purchase-similarities-sims-feature']/div[boolean(@data-p13n-asin-metadata)]/@data-p13n-asin-metadata")
        also_bought = util.fjson(also_bought)
        if isinstance(also_bought, dict):
            also_bought_asin = [k for k, v in also_bought.items()]
        else:
            also_bought_asin = []
        detail["also_bought_asin"] = also_bought_asin

        special_offers = div_centerCol.xpath("//div[@id='quickPromoBucketContent']/div[@class='content']/ul/li/text()")
        special_offers = util.fstring(special_offers)
        detail["special_offers"] = special_offers

        similar_items = div_centerCol.xpath("//div[@id='HLCXComparisonWidget_feature_div']/table[@id='HLCXComparisonTable']/td[boolean(@data-asin)]/@data-asin")
        detail["similar_items"] = similar_items

        customer_also_viewed = div_centerCol.xpath("//div[@id='desktop-dp-sims_session-similarities-sims-feature']/div/@data-a-carousel-options")
        customer_also_viewed_asin = util.fjson(customer_also_viewed)["ajax"]["id_list"] if isinstance(util.fjson(customer_also_viewed), dict) else util.fjson(customer_also_viewed)
        detail["customer_also_viewed_asin"] = customer_also_viewed_asin

        detail_result = Result.from_task(task=current_task, details=detail)

        return detail_result, None


if __name__ == "__main__":
    import json
    from framework.mock import MockResponse, MockTask
    parser = DpParser()
    # sampler_local_html = "./samples/dp_B01N1Z2HUS.html"
    sampler_local_html = "./samples/dp_B089DR29T6_no_nodePath.html"
    response = MockResponse(sampler_local_html)
    parser.parse(response, MockTask(None))